//
//  CSNavigationViewController.m
//  ChinaShip
//
//  Created by yyk on 2019/1/23.
//  Copyright © 2019年 ChinaShip. All rights reserved.
//

#import "CSNavigationViewController.h"
#import "CSSectionListViewController.h"
@interface CSNavigationViewController ()<UIGestureRecognizerDelegate>

@end

@implementation CSNavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //系统的返回手势
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        // 获取系统自带滑动手势的target对象
        id target = self.interactivePopGestureRecognizer.delegate;
        // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
        UIScreenEdgePanGestureRecognizer *pan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
        pan.edges = UIRectEdgeLeft;//左侧
        // 设置手势代理，拦截手势触发
        pan.delegate = self;
        // 给导航控制器的view添加全屏滑动手势
        [self.view addGestureRecognizer:pan];
        // 禁止使用系统自带的滑动手势
        self.interactivePopGestureRecognizer.enabled = NO;   // 手势的代理设置为self
    }
}

- (void)handleNavigationTransition:(UIGestureRecognizer *)recoginzer{
    [self popViewControllerAnimated:YES];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    UIViewController *vc = self.viewControllers.lastObject;
    if ([vc isKindOfClass:[CSSectionListViewController class]]) {
        return NO;
    }
    // 注意：只有非根控制器才有滑动返回功能，根控制器没有。
    // 判断导航控制器是否只有一个子控制器，如果只有一个子控制器，肯定是根控制器
    if (self.childViewControllers.count == 1) {
        // 表示用户在根控制器界面，就不需要触发滑动手势，
        return NO;
    }
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    UIViewController *vc = self.viewControllers.lastObject;
    if ([vc isKindOfClass:[CSSectionListViewController class]]) {
        return NO;
    }
    if (self.viewControllers.count <= 1){
        return NO;
    }
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
